package component.untils
{
	import common.core.Txt;
	import common.ext.filter.FilterLib;
	import common.ext.font.TextFormatLib;
	import common.manager.Resource;
	
	import component.ComponentLib;
	import component.show.containers.*;
	import component.show.containers.tree.Tree;
	
	import fl.containers.ScrollPane;
	import fl.containers.UILoader;
	import fl.controls.*;
	import fl.controls.dataGridClasses.DataGridColumn;
	
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	import flash.text.TextFormat;

	public class DeserializeUIXML
	{
	   private var m_collection : Object;
	   private var m_rootItem : ComponentItem;
	
		public function DeserializeUIXML()
		{
			m_collection = new Object;
		}
		
		public function destory() : void
		{
			m_collection = null;
			m_rootItem = null;
		}
		
		public function get collection() : Object
		{
			return m_collection;
		}
		
		public function set collection(value:Object) : void
		{
			m_collection = value;
		}
		
		public function get rootItem() : ComponentItem
		{
			return m_rootItem;
		}
		
		/**
		 * 递归解析UI XML
		 * @param xmlInfo
		 * @param displayObject 
		 */		
		public function recursionXML(xmlInfo : XML,displayObject : Sprite,item:ComponentItem = null):void
		{
			if(xmlInfo.name().localName == "Component")
			{
				if(!displayObject)
				{
					displayObject = new UIFrame
				}
				
				var posLst : Array = xmlInfo.@scale9Grid.toString().split(/,/);
				
				var bitmapD : BitmapData;
				var skinName : String = xmlInfo.@skin.toString();
				
				if(posLst.length == 4)
				{
					var rec : Rectangle = new Rectangle(posLst[0],posLst[1],posLst[2],posLst[3]);
					
					(displayObject as UIFrame).scale9Grid = rec;
				}
				
				if(skinName)
				{
					(displayObject as UIFrame).skinName = skinName;
				}
				
				displayObject.x = Number(xmlInfo.@x);
				displayObject.y = Number(xmlInfo.@y);
				displayObject.width = Number(xmlInfo.@width);
				displayObject.height = Number(xmlInfo.@height);
				displayObject.name = xmlInfo.@name.toString();
				
				m_rootItem = new ComponentItem;
				m_rootItem.item = displayObject;
				item = m_rootItem;
			}
			
			for each(var itemXML : XML in xmlInfo.elements())
			{
				var comClass : Class  = ComponentLib.lib[itemXML.name().localName];
				var compItem : ComponentItem = new ComponentItem;
				
				if(comClass)
				{
					var comp : * = new comClass as Sprite;
					
					if(comp is UIFrame)
					{
						var posLst1 : Array = itemXML.@scale9Grid.toString().split(/,/);
						
						var bitmapD1 : BitmapData;
						var skinName1 : String = itemXML.@skin.toString();
						
						if(posLst1.length == 4)
						{
							var rec1 : Rectangle = new Rectangle(posLst1[0],posLst1[1],posLst1[2],posLst1[3]);
							
							(comp as UIFrame).scale9Grid = rec1;
						}
						
						(comp as UIFrame).skinName = skinName1;
					}
					else if(comp is Label)
					{
						var txtId : String = itemXML.@htmlText
				
						var textFormat : String  = itemXML.@TextFormat.toString();	
						
						var txt : String = Txt.getTxt(txtId);
						
						if(txt && !(txt is Function))
						{
							(comp as Label).htmlText = txt;
							compItem.txtId = txtId;
						}
						else
						{
							(comp as Label).htmlText = txtId;
						}
				
						if(textFormat && textFormat!="null")
						{
							(comp as Label).setStyle("textFormat",TextFormatLib[textFormat]);
							compItem.textFormatName = textFormat;
						}
					}
					else if(comp is CheckBox)
					{
						(comp as CheckBox).label = itemXML.@htmlText;
					}
					else if(comp is Button)
					{
						(comp as Button).label = itemXML.@htmlText;
					}
					else if(comp is Image)
					{
						(comp as Image).skinName = itemXML.@skin;
					}
					else if(comp is TextInput)
					{
						(comp as TextInput).htmlText = itemXML.@htmlText;
					}
					else if(comp is TextArea)
					{
						txtId  = itemXML.@htmlText
						
						textFormat  = itemXML.@TextFormat.toString();	
						
						txt  = Txt.getTxt(txtId);
						
						if(txt && !(txt is Function))
						{
							(comp as TextArea).htmlText = txt;
							compItem.txtId = txtId;
						}
						else
						{
							(comp as TextArea).htmlText = txtId;
						}
						
						if(textFormat && textFormat!="null")
						{
							(comp as TextArea).setStyle("textFormat",TextFormatLib[textFormat]);
							compItem.textFormatName = textFormat;
						}
					}
					else if(comp is List)
					{
						
					}
					else if(comp is DataGrid)
					{
						var colStr : String = itemXML.@colums.toString();
						var reg : RegExp = /\[[^\]]*\]/g;
						var cols : Array = colStr.match(reg);
						var length : int = cols.length;
						var columns : Array = [];
						
						for(var i:int=0;i<length;i++)
						{
							var dgCol : DataGridColumn = new DataGridColumn;
							var str : String = cols[i].toString().replace("[","");
							str = str.replace("]","");
							var conLst : Array = str.split(/,/);
							
							dgCol.headerText = conLst[0];
							dgCol.resizable = (conLst[1] == 1);
							dgCol.sortable = (conLst[2] == 1);
							
							columns.push(dgCol);
						}
						
						(comp as DataGrid).columns = columns;
					}
					else if(comp is UILoader)
					{
						
					}
					else if(comp is SimpleBtn)
					{
						(comp as SimpleBtn).renderer = Resource.getClass(itemXML.@skin,"UISkin");
					}
					else if(comp is ToggleButton)
					{
						(comp as ToggleButton).simpleButtonRenderer = Resource.getClass(itemXML.@skin,"UISkin");
					}
					else if(comp is Tree)
					{
						
					}
					else if(comp is ScrollPane)
					{
						
					}
					
					comp.name = itemXML.@name.toString();
					comp.x = Number(itemXML.@x);
					comp.y = Number(itemXML.@y);
					comp.width = Number(itemXML.@width);
					comp.height = Number(itemXML.@height);
					
					var filterName : String = itemXML.@filterName.toString();
					if(filterName)
					{
						comp.filters = [FilterLib[filterName]];
					}
					
					compItem.item = comp;
					compItem.parent = item;
					
					displayObject.addChild(comp);
					
					m_collection[comp.name] = comp;
					
					recursionXML(itemXML,comp,compItem);
				}
			}
		}
	}
}